/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.security.utils.EncryptUtil;
import com.jspx.sober.TableModels;
import com.jspx.utils.StringUtil;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-3-21
 * Time: 下午8:39
 *
 */
public abstract class SoberUtil {
    final private static Logger log = LoggerFactory.getLogger(SoberUtil.class);
    final public static String cache_trem_list = "_list_";
    final public static String cache_trem_load = "_load_";

    private SoberUtil()
    {

    }

    /**
     *
     * @param soberTable 表对象
     * @param fields 字段
     * @return  字段安全检测 true 表示继续运行，false 表示过滤这个字段条件，应为这个字段不存在
     */
    public static boolean containsFields(TableModels soberTable, String[] fields)
    {
        if (fields==null) return  true;
        if (soberTable==null) return true;
        for (String field:fields)
        {
            if (!soberTable.containsField(field))
            {
                log.debug(soberTable.getTableName() + "  " + soberTable.getTableCaption() + " field:" + field);
                return  false;
            }
        }
        return  true;
    }

    public static String getLoadKey(Class aClass, Serializable field, Serializable serializable, boolean loadChild) {
        StringBuilder sb = new StringBuilder(aClass.getName());
        sb.append(cache_trem_load);
        StringBuilder sbt = new StringBuilder(aClass.getName());
        sb.append(EncryptUtil.getMd5(sbt.append(field).append("_e_").append(serializable).append("_l_").append(loadChild).toString()));
        return sb.toString();
    }

    public static String getListKey(Class aClass, String term, boolean loadChild) {
        StringBuilder sb = new StringBuilder(aClass.getName());
        sb.append(cache_trem_list);
        StringBuilder sbt = new StringBuilder(aClass.getName());
        sb.append(EncryptUtil.getMd5(sbt.append(term).append("_l_").append(loadChild).toString()));
        return sb.toString();
    }

    public static String getGroupListKey(Class aClass, String term)
    {
        StringBuilder sb = new StringBuilder(aClass.getName());
        sb.append(cache_trem_list).append(EncryptUtil.getMd5(term));
        return sb.toString();
    }

    public static String toTypeString(Object obj) {
        if (obj==null) return StringUtil.empty;
        if (obj instanceof String) return StringUtil.quote(obj.toString(),false);
        return obj.toString();
    }
}